<template>
    <div>
      <div class="goodsindex" v-loading="visiable">
        <!-- 搜索条件 -->
        <el-row :gutter="20" class="goodsindex-queryInfo">
          <el-col :xs="8" :sm="5" :md="5" :lg="5" :xl="5">
            <!-- <el-input
              class="goodsindex-queryInfo-li"
              v-model="queryInfo.userName"
              clearable
              size="small"
              placeholder="请输入创建人名称"
            ></el-input>
          </el-col>
          <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2">
            <el-button
              type="primary"
              class="goodsindex-queryInfo-li"
              size="small"
              @click="select"
              >搜索</el-button
            > -->
          </el-col>
          <el-col
            :xs="6"
            :sm="4"
            :md="3"
            :lg="2"
            :xl="2"
            v-if="isAuth('flow:apply:save')"
          >
            <el-button
              type="primary"
              class="goodsindex-queryInfo-li"
              size="small"
              @click="addOrUpdatehandle()"
              >添加</el-button
            >
          </el-col>
        </el-row>
        <!-- 检索结果 -->
        <el-row :gutter="20" class="goodsindex-list">
          <el-col :span="24" class="goodsindex-list-col">
            <el-table
              :data="menuList"
              style="width: 100%"
              row-key="treeId"
              :height="tableMaxHeight"
              border
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            >
              <el-table-column prop="name" label="流程名称" header-align="center">
                <template #default="scope">
                  <!-- <span v-if="scope.row.treeLevel == 1 || !scope.row.treeLevel">{{
                    scope.row.name
                  }}</span> -->
                  <span>{{ scope.row.name }}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="state"
                label="状态"
                header-align="center"
                align="center"
              >
                <template #default="scope">
                  <span v-if="scope.row.state == 1">已保存</span>
                  <span v-if="scope.row.state == 2">审批中</span>
                  <span v-if="scope.row.state == 3">驳回</span>
                  <span v-if="scope.row.state == 4">已归档</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="createUserName"
                label="创建人"
                header-align="center"
                align="center"
              >
              </el-table-column>
              <el-table-column
                prop="createTime"
                label="创建时间"
                header-align="center"
                align="center"
              >
              </el-table-column>
              <!-- <el-table-column
                        prop="orderNum"
                        label="排序号"
                        header-align="center"
                          align="center"
                        >
                      </el-table-column> -->
              <el-table-column
                label="操作"
                align="center"
                width="380"
                fixed="right"
              >
                <template #default="scope">
                  <el-button
                    v-if="isAuth('flow:apply:submit')"
                    size="mini"
                    @click="
                      submit(scope.row)
                    "
                    :disabled="scope.row.state == 2||scope.row.state == 4"
                    >提交</el-button
                  >
                  <el-button
                    v-if="isAuth('flow:apply:updata')"
                    size="mini"
                    @click="
                      addOrUpdatehandle(scope.row.objectFlowId)
                    "
                    :disabled="scope.row.state == 2||scope.row.state == 4"
                    >编辑</el-button
                  >
                  <el-button
                    v-if="isAuth('flow:apply:delete')"
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.row.objectFlowId)"
                    :disabled="scope.row.state == 2"
                    >删除</el-button
                  >
                  <el-button
                    v-if="isAuth('flow:apply:look')"
                    size="mini"
                    @click="handleLook(scope.row.objectFlowId)"
                    >查看</el-button
                  >
                  <el-button
                    v-if="isAuth('flow:apply:flow')"
                    size="mini"
                    @click="approvalProcessLook(scope.row.processInstanceId)"
                    >审批流程</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <!-- 分页 -->
        <el-row :gutter="20">
          <el-col :span="24" style="display: flex; justify-content: flex-end">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageIndex"
              :page-sizes="[1, 10, 20, 50, 100]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalPage"
            >
            </el-pagination>
          </el-col>
        </el-row>
        <userAddorUpdata
          ref="userAddorUpdata"
          @getMenuList="getMenuList"
        ></userAddorUpdata>
        <look ref="look"></look>
        <approvalProcess ref="approvalProcess"></approvalProcess>
      </div>
    </div>
  </template>
  
  <script>
  import headers from "../../components/header/header.vue";
  import { treeDmodelataTranslate } from "../../utils/treeDataTranslate.js";
  import userAddorUpdata from "./apply/addorupdate.vue";
  import look from "./apply/look.vue";
  import approvalProcess from "./apply/approvalProcess.vue";
  export default {
    components: {
      userAddorUpdata,
      headers,
      look,
      approvalProcess
    },
    data() {
      return {
        tableData: [],
        dataForm: {},
        menuList: [],
        pageIndex: 1,
        pageSize: 20,
        totalPage: 0,
        addOrUpdateVisible: false,
        tableHeight: "",
        queryInfo: {
          userName: "",
        },
        visiable:false
      };
    },
    computed: {
      tableMaxHeight() {
        return window.innerHeight - 180 + "px";
      },
    },
    created() {
      this.getMenuList();
    },
    methods: {
      //搜素
      async select() {
        await this.getMenuList();
      },
      getMenuList() {
        this.Sevice({
          url: "ad/object/flow/list",
          method: "get",
          params: {
            userName: this.queryInfo.userName,
          },
        })
          .then(async (res) => {
            if (res && res.code == 200) {
              // res.list.sort(function (a, b) {
              //   return a.createTime < b.createTime ? 1 : -1;
              // });
              this.menuList = res.list
              this.totalPage = this.menuList.length;
              this.menuList = this.menuList.slice(
                (this.pageIndex - 1) * this.pageSize,
                this.pageIndex * this.pageSize
              );
            } else {
              this.menuList = [];
              this.totalPage = 0;
            }
          })
          .catch((err) => {
            console.log(err);
          });
      },
      // 新增 / 修改
      addOrUpdatehandle(objectFlowId) {
        this.$nextTick(() => {
          this.$refs.userAddorUpdata.init(objectFlowId);
        });
      },
      handleLook(objectFlowId){
        this.$nextTick(() => {
          this.$refs.look.init(objectFlowId);
        });
      },
      approvalProcessLook(processInstanceId){
        this.$nextTick(() => {
          this.$refs.approvalProcess.init(processInstanceId);
        });
      },
      //删除
      handleDelete(id) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.Sevice({
              url: "ad/object/flow/delete/" + id,
              method: "post",
            })
              .then((res) => {
                if (res && res.code == 200) {
                  this.$message({
                    type: "success",
                    message: "删除成功!",
                  });
                  this.getMenuList();
                }
              })
              .catch((err) => {
                console.log(err);
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      // 分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getMenuList();
      },
      handleCurrentChange(val) {
        this.pageIndex = val;
        this.getMenuList();
      },
      submit(data){
        let approveReqVO={
          id:data.objectFlowId,
          taskId:data.taskId
        }
        this.visiable=true
        this.Sevice({
              url: "ad/object/flow/submit",
              data:approveReqVO,
              method: "post",
            })
              .then((res) => {
                if (res && res.code == 200) {
                  this.$message({
                    type: "success",
                    message: "提交成功!",
                  });
                  this.getMenuList();
                }
                this.visiable=false
              })
              .catch((err) => {
                console.log(err);
                this.visiable=false
              });
      }
    },
  };
  </script>
  
  <style scoped>
  .goodsindex {
    width: 100%;
    min-height: 100%;
    padding: 15px;
    box-sizing: border-box;
  }
  /* 搜索 */
  .goodsindex-queryInfo {
    margin-bottom: 10px;
  }
  .goodsindex-queryInfo-li {
    width: 100%;
    height: auto;
    margin-bottom: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  /* 列表 */
  .goodsindex-list {
    width: 100%;
    height: auto;
    margin-left: 0 !important;
    margin-right: 0 !important;
    margin-bottom: 20px;
  }
  .goodsindex-list-col {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }
  /* 分页 */
  .goodsindex-page-box {
    width: 100%;
    height: auto;
    display: flex;
    justify-content: flex-end;
  }
  </style>
  